package com.xindaoapp.happypet.fragments;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.WeibaDetailAdapter;
import com.xindaoapp.happypet.bean.NiuniuRecord;
import com.xindaoapp.happypet.ui.BaseFragment;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 尾巴明细
 * 
 */
public class WeibaDetailFragment extends BaseFragment {

    /**
     * 
     * @param fid
     * @param isBack 是否点击之后返回
     * @return
     */

    public static Fragment newInstance() {
        Fragment fragment = new WeibaDetailFragment();
        Bundle bundle = new Bundle();
        // bundle.putString(Constants.KEY_GROUP_FID, fid);
        // bundle.putBoolean(Constants.KEY_IS_CLICK_TO_BACK, isBack);
        fragment.setArguments(bundle);
        return fragment;
    }

    private PullToRefreshListView vPullToRefreshListView;
    private View mHeadView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_weiba_detail, container, false);
        vPullToRefreshListView = (PullToRefreshListView) rootView.findViewById(R.id.listview);
        vPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mHeadView = inflater.inflate(R.layout.item_myrecord_head, null);
        if (vPullToRefreshListView.getRefreshableView().getHeaderViewsCount() <= 1) {
            vPullToRefreshListView.getRefreshableView().addHeaderView(mHeadView);
            vPullToRefreshListView.getRefreshableView().setAdapter(null);
        }
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        vPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                loadDatas();
            }
        });

    }

    @Override
    protected void loadDatas() {
        super.loadDatas();


        /**
         * 牛币记录
         */
        getMoccaApi().getNiuniuRecord(Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                new IRequest<List<NiuniuRecord>>() {

                    @Override
                    public void request(List<NiuniuRecord> t) {
                        if (t != null) {
                            onDataArrived(true);
                            if (t.size() == 0) {
                                onDataArrivedEmpty();
                            } else {
                                vPullToRefreshListView.setAdapter(new WeibaDetailAdapter(
                                        getActivity(), t, Constants.PAGE_SIZE,
                                        R.layout.item_myrecord, R.layout.item_loading));
                            }
                        } else {
                            onDataArrived(false);
                        }
                        vPullToRefreshListView.onRefreshComplete();
                    }
                });


    }
}
